package com.cloud.phone.base;


import android.app.Activity;
import android.app.Application;

import com.blankj.utilcode.util.SPUtils;
import com.cloud.phone.constants.Constants;
import com.cloud.phone.http.ApiService;
import com.cloud.phone.http.CloudApi;
import com.cloud.phone.utils.UMengUtils;
import com.efs.sdk.net.OkHttpInterceptor;
import com.efs.sdk.net.OkHttpListener;
import com.umeng.commonsdk.UMConfigure;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class BaseApp extends Application {
    public static BaseApp app;
    private List<Activity> activities;
    public static int flag = -1;
    private static ApiService SERVICE;
    @Override
    public void onCreate() {
        super.onCreate();
        //清空token
        SPUtils.getInstance().put("token","");
        UMConfigure.preInit(this, Constants.INSTANCE.getUM_APP_KEY(), Constants.INSTANCE.getUM_APP_CHANNEL());
        app=this;
        activities = new ArrayList<>();
        flag = 0;
        UMengUtils.INSTANCE.init(this);
    }

    public static boolean isAppLive() {
        return flag == 0;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        flag = -1;
    }

    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    public void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    // 清除所有activity的引用
    public void clearActivities() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        activities.clear();
    }

}
